package com.region.http.client.model;

import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;

/**
 * @author liujieyu
 * @date 2024/7/21 19:37
 * @description
 */
public class ResponseData {

    /**
     * The headers of response
     */
    private Map<String, String[]> headers;

    /**
     * The name and value in the value of response header
     */
    private Map<String, NameValuePair[]> nameValuePairs;

    /**
     * Response body data
     */
    private byte[] body;

    /**
     * The return type
     */
    private Type type;

    /**
     * The array of annotation on the function
     * The issue is https://gitee.com/LiuJieYuuuu/iagent-parent/issues/I9DI1H
     */
    private Annotation[] annotations;

    private ResponseData() {
    }

    public Map<String, String[]> getHeaders() {
        return headers;
    }

    public ResponseData addHeader(String name, String value) {
        if (this.headers == null) {
            this.headers = new HashMap<>(16);
        }
        if (headers.containsKey(name)) {
            String[] values = headers.get(name);
            String[] newValues = new String[values.length + 1];
            System.arraycopy(values, 0, newValues, 0, values.length);
            newValues[values.length] = value;
            headers.put(name, newValues);
        } else {
            headers.put(name, new String[]{value});
        }
        return this;
    }

    public Map<String, NameValuePair[]> getNameValuePairs() {
        return nameValuePairs;
    }

    public ResponseData addNameValuePair(String headerName, String name, String value) {
        if (this.nameValuePairs == null) {
            this.nameValuePairs = new HashMap<>(16);
        }
        if (nameValuePairs.containsKey(headerName)) {
            NameValuePair[] pairs = nameValuePairs.get(headerName);
            NameValuePair[] newPairs = new NameValuePair[pairs.length + 1];
            System.arraycopy(pairs, 0, newPairs, 0, pairs.length);
            newPairs[pairs.length] = new NameValuePair(name, value);
            nameValuePairs.put(headerName, newPairs);
        } else {
            nameValuePairs.put(headerName, new NameValuePair[]{new NameValuePair(name, value)});
        }
        return this;
    }

    public byte[] getBody() {
        return body;
    }

    public ResponseData setBody(byte[] body) {
        this.body = body;
        return this;
    }

    public Type getType() {
        return type;
    }

    public ResponseData setType(Type type) {
        this.type = type;
        return this;
    }

    public Annotation[] getAnnotations() {
        return annotations;
    }

    public ResponseData setAnnotations(Annotation[] annotations) {
        this.annotations = annotations;
        return this;
    }

    public static ResponseData getInstance() {
        return new ResponseData();
    }

    /**
     * The element of response header
     */
    public static class NameValuePair {

        private String name;

        private String value;

        public NameValuePair(String name, String value) {
            this.name = name;
            this.value = value;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getValue() {
            return value;
        }

        public void setValue(String value) {
            this.value = value;
        }
    }

}
